package com.example.androidbrowserpro.setting;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;

import com.example.androidbrowserpro.R;
import com.google.android.material.textfield.TextInputEditText;

public class SettingDialog extends AlertDialog implements View.OnClickListener {
    Button mBtnCancel, mBtnConnect,mBtnClose;
    Context mContext;
    Activity mActivity;
    TextInputEditText urlInput;
    private final WebView _webView;
    private final String _url;
   // InputMethodManager imm;

    public SettingDialog(Context context, Activity activity, WebView webVie, String url) {
        super(context);
        mContext = context;
        mActivity = activity;
        this._webView = webVie;
        this._url = url;
       // imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
       // imm.showSoftInput(webVie, 100);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        mBtnCancel = findViewById(R.id.btn_cancel);
        mBtnCancel.setOnClickListener(this);
        _setBtFocus(mBtnCancel);
        mBtnConnect = findViewById(R.id.btn_enter);
        mBtnConnect.setOnClickListener(this);
        _setBtFocus(mBtnConnect);
        mBtnClose = findViewById(R.id.btn_close);
        mBtnClose.setOnClickListener(this);
        _setBtFocus(mBtnClose);
        urlInput = findViewById(R.id.urlInput);
        urlInput.setText(this._url);

        final SpannableStringBuilder style = new SpannableStringBuilder();

        //showSoftInputMethod(_webView,mContext);
        //设置文字
        style.append("请您在使用前点击阅读《XXXX隐私政策》，如果同意，请点击下方“同意”按钮开始使用软件。");

        //设置部分文字点击事件
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(mContext, "触发点击事件!", Toast.LENGTH_SHORT).show();
            }
        };
        style.setSpan(clickableSpan, 11, 20, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        //设置部分文字颜色
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#0000FF"));
        style.setSpan(foregroundColorSpan, 11, 20, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);



    }
    /**
     * 显示键盘
     */
    public void showSoftInputMethod(View view, Context mContext) {
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            view.requestFocus();
            imm.showSoftInput(view, 0);
        }
    }

    private  void  _setBtFocus(Button button){
        button.setTextColor(Color.BLUE);
        button.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            public void onFocusChange(View v, boolean hasFocus) {

                if (hasFocus) {

                    button.setTextColor(Color.RED);
                    button.setBackgroundColor(Color.BLUE);

                }else {

                    button.setTextColor(Color.BLUE);
                    button.setBackgroundColor(Color.WHITE);
                }

            }

        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                this.dismiss();
                break;
            case R.id.btn_enter:
                SharedPreferences sp = mContext.getSharedPreferences("webViewSetting", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("url", urlInput.getText().toString());
                editor.apply();
                this._webView.loadUrl(urlInput.getText().toString());
                this.dismiss();
                this._webView.clearCache(true);
                break;
            case R.id.btn_close:
                android.os.Process.killProcess(android.os.Process.myPid());
                //mActivity.finish();
                //System.exit(0);
                //this.dismiss();
                break;
            default:
                break;
        }
    }
}

